﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for CKVN.
    /// </summary>
    public class TAINHACCHUONG
    {

        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();
        public TAINHACCHUONG(SMS_MO MO)
        {
            WMO = MO;
        }

        public TAINHACCHUONG()
        {
        }
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string strReturnMsg = GetReturnMsg(msg);
            if (msg.Length == 1)
            {
                strReturnMsg = GetReturnMsg(msg);
                WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
            }
            else
            {
                //thong bao lai
                string str= "Ban da gui tang bai hat co ma so " + msg[0] + " toi so dien thoai " + msg[1] +".Chuc ban co nhung giay phut that vui ve!";
                WMT.AddMessage(WMO.UserID,str, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
                //nhan tin cho ban
                //xac dinh server
                string strconnec = "";
                string sdtnhan ="84" + msg[1].Trim().Substring(1, msg[1].Trim().Length - 1);
                string telco = GetTelcoName(sdtnhan);
                if (telco == "VIETTEL")
                { strconnec = ConfigurationSettings.AppSettings["SMSConnStrViettel"].ToString(); }
                else if (telco == "MOBI")
                { strconnec = ConfigurationSettings.AppSettings["SMSConnStrMobi"].ToString(); }
                else if (telco == "VINA")
                { strconnec = ConfigurationSettings.AppSettings["SMSConnStrVina"].ToString(); }
                //insert vao MT
                InsertMT(strconnec, 0, sdtnhan, WMO.ServiceID, WMO.CommandCode, strReturnMsg, 0, 1, 0);
                return;

            }
            return;
        }
        private string GetReturnMsg(string[] msg)
        {
            string msgBody = "";
            if ((WMO.ServiceID.Replace("04", "") == "6372") || (WMO.ServiceID.Replace("04", "") == "6572") || (WMO.ServiceID.Replace("04", "") == "6772") || (WMO.UserID == "84902222776"))
            {
                if (msg.Length == 1)//tai ve may
                {
                    msgBody = GetMsgStockFromURL("http://tainhacchuong.net/sms.php?mbh=" + msg[0] + "&ds=" + WMO.ServiceID + "&sdtgui=" + WMO.UserID);
                }
                else//tang nguoi khac
                {
                    msgBody = GetMsgStockFromURL("http://tainhacchuong.net/sms.php?mbh=" + msg[0] + "&ds=" + WMO.ServiceID + "&sdtgui=" + WMO.UserID + "&sdtnhan=" + msg[1]);
                }
            }
            else
            {
                msgBody = "De tai nhac chuong ban vui long soan tin NC <MaBaiHat> gui 6372. De tang cho ban be soan: NC <MaBaiHat> <SdtNguoiNhan> gui 6372";
            }
            return msgBody;
        }
        //GetDataFromURL
        private string GetMsgStockFromURL(string url)
        {
            string strval = "";
            WebRequest WQob = WebRequest.Create(url);
            WebResponse WPob = WQob.GetResponse();
            StreamReader SRob = new StreamReader(WPob.GetResponseStream());
            strval = SRob.ReadToEnd();
            WQob.Abort();
            return strval;
        }
        private string GetTelcoName(object userid)
        {
            string str2number = userid.ToString().Trim().Substring(2, 2);
            string str3number = userid.ToString().Trim().Substring(2, 3);
            string telconame = "";
            if (str2number == "97" || str2number == "98" || str3number == "163" || str3number == "164" || str3number == "165" || str3number == "166" || str3number == "167" || str3number == "168" || str3number == "169")
                telconame = "VIETTEL";
            else if (str2number == "91" || str2number == "94" || str3number == "123" || str3number == "125" || str3number == "127" || str3number == "129")
                telconame = "VINA";
            else if (str2number == "90" || str2number == "93" || str3number == "120" || str3number == "121" || str3number == "122" || str3number == "126" || str3number == "128")
                telconame = "MOBI";
            else
                telconame = "Unname";
            return telconame;
        }
        private int InsertMT(string strconnec, int mo_id, string User_id, string ServiceID, string CommandCode, string Message, int RequestID, int MsgType, int ContentType)
        {
            int val = 0;
            string reqSQL = "INSERT INTO SMS_MT(MO_ID,UserID,ReceiverUserID,ServiceID,CommandCode,Message,RequestID,MsgType,ContentType,RequestTime)";
            reqSQL += " VALUES(@MO_ID,@UserID,@ReceiverUserID,@ServiceID,@CommandCode,@Message,@RequestID,@MsgType,@ContentType,@RequestTime)";
            try
            {
                SqlConnection conn = new SqlConnection(strconnec);
                conn.Open();
                SqlCommand oCommand = new SqlCommand(reqSQL, conn);
                oCommand.Parameters.Clear();
                oCommand.Parameters.AddWithValue("@MO_ID", mo_id);
                oCommand.Parameters.AddWithValue("@UserID", User_id);
                oCommand.Parameters.AddWithValue("@ReceiverUserID", "");
                oCommand.Parameters.AddWithValue("@ServiceID", ServiceID);
                oCommand.Parameters.AddWithValue("@CommandCode", CommandCode);
                oCommand.Parameters.AddWithValue("@Message", Message);
                oCommand.Parameters.AddWithValue("@RequestID", RequestID);
                oCommand.Parameters.AddWithValue("@MsgType", MsgType);
                oCommand.Parameters.AddWithValue("@ContentType", ContentType);
                oCommand.Parameters.AddWithValue("@RequestTime", DateTime.Now);
                oCommand.ExecuteNonQuery();
                conn.Close();
                val = 1;
            }
            catch (Exception myErr)
            {
                val = 0;
            }
            return val;
        }        
        
    }
}
